<?
include 'db_info.php';
include 'alert.php'; // includes alert window control functions


// get data from 'make.php' by 'POST' method
$teamname = $_POST['teamname'];
$teampw = $_POST['teampw'];
$teampw2 = $_POST['teampw2'];
$vocal = $_POST['vocal'];
$first = $_POST['first'];
$second = $_POST['second'];
$bass = $_POST['bass'];
$drum = $_POST['drum'];
$keyboard = $_POST['keyboard'];
$others = $_POST['others'];
$isTemp = $_POST['isTemp'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>팀 생성 - 합주시간표</title>
<style type="text/css">
body { color: black; font-size: 10pt; font-family: 맑은고딕, 돋움;}
th { color: white; padding: 10px; font-size: 15pt; font-family: 맑은고딕, 돋움; font-weight: bold; background: #71A7F0;}
td { padding: 5px; }
a { color: #FF4848; font-weight: bold; }
</style>
</head>
<body>

<?
// basic error check start
// filter blank field and different password
if ($teamname == "") { Alert_back("합주팀 이름을 적어주세요"); }
if ( strpos($teamname, "\"") || strpos($teamname, "'")) Alert_back("팀 이름에 따옴표를 포함할 수 없습니다");
if ($teampw == "") { Alert_back("합주팀 관리용 비밀번호를 적어주세요"); }
if ($teampw != $teampw2) { Alert_back("비밀번호가 일치하지 않습니다"); }

// duplicated teamname
$teamname_check = mysqli_query($connect, "SELECT * FROM team WHERE teamname='$teamname'");
if ( mysqli_num_rows($teamname_check) != 0 ) {
	mysqli_free_result($teamname_check);
	Alert_back("이미 사용중인 팀이름입니다");
}
// basic error check end

// day of week
$week = array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
$week_korean = array("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일");
// store time of each day (starts from monday)
$start = array();
$end = array();

// count the number of success
$success_counter = 0;

for ($i = 0; $i < 7; $i++) {
	// make string for $_POST index of start time
	$start_string = $week[$i]."_start";
	// store start time of each day
	$start[$i] = $_POST["$start_string"];

	// make string for $_POST index of end time
	$end_string = $week[$i]."_end";
	// store start time of each day
	$end[$i] = $_POST["$end_string"];

	// no input, no error, no insert
	if ($start[$i] == 0 && $end[$i] == 0) continue;

	// basic time error
	if ( $start[$i] > $end[$i] ) { Alert_back( "$week_korean[$i]: 시작/종료 시각 입력 에러" ); }

	// time duplication error
	$query_string = "SELECT * FROM team_time WHERE $start_string >= $start[$i] && $start_string < $end[$i]";
	$query = mysqli_query($connect, $query_string);
	$timecheck1 = mysqli_num_rows($query);
	mysqli_free_result($query);
	$query_string = "SELECT * FROM team_time WHERE $start_string <= $start[$i] && $end_string > $start[$i]";
	$query = mysqli_query($connect, $query_string);
	$timecheck2 = mysqli_num_rows($query);
	mysqli_free_result($query);
	// either timecheck1 or timecheck2 != 0, it means that time duplication occurred.
	if ($timecheck1 || $timecheck2)  { Alert_back("시간이 겹치는 팀이 있습니다"); }

	// success!
	$success_counter++;

	// if passed all the error checks above, then update timetable
	if (!$isTemp) {
		// store team into 'timetable' table
		for ($counter = $start[$i]; $counter < $end[$i]; $counter += 50) {
			$query_string = "UPDATE timetable SET $week[$i]='$teamname' WHERE time='$counter'";
			$query = mysqli_query($connect, $query_string);
		}
	}
	else {
		// store team into 'timetable_temp' table
		for ($counter = $start[$i]; $counter < $end[$i]; $counter += 50) {
			$query_string = "UPDATE timetable_temp SET $week[$i]='$teamname' WHERE time='$counter'";
			$query = mysqli_query($connect, $query_string);
		}
	}
}

if ($success_counter) { // $success_counter == 0 means all time input was null
	// if passed all the error checks above, then insert
	// store team info row into 'team' table
	$query_string = "INSERT INTO team (teamname, teampw, vocal, first, second, bass, drum, keyboard, others, isTemp) 
					VALUES('$teamname', '$teampw', '$vocal', '$first', '$second', '$bass', '$drum', '$keyboard', '$others', '$isTemp')";
	$query = mysqli_query($connect, $query_string);

	// store time info row into 'team_time' table
	$query_string = "INSERT INTO team_time (teamname, monday_start, monday_end, tuesday_start, tuesday_end, wednesday_start, wednesday_end, 
								thursday_start, thursday_end, friday_start, friday_end, saturday_start, saturday_end, sunday_start, sunday_end) 
						VALUES('$teamname', '$start[0]', '$end[0]', '$start[1]', '$end[1]', '$start[2]', '$end[2]',
								'$start[3]', '$end[3]', '$start[4]', '$end[4]', '$start[5]', '$end[5]', '$start[6]', '$end[6]')";
	$query = mysqli_query($connect, $query_string);

	Alert_index( "합주팀 저장 완료" );
}
else { Alert_back("시작/종료 시각 입력 에러"); }
?>

</body>
</html>